home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16.lha / Python-1.6 / Lib / Python1.6 / distutils / util.py < prev    next >
Encoding:
Python Source  |  2000-08-08  |  8.0 KB  |  232 lines

  1. """distutils.util
  2.  
  3. Miscellaneous utility functions -- anything that doesn't fit into
  4. one of the other *util.py modules."""
  5.  
  6. # created 1999/03/08, Greg Ward
  7.  
  8. __revision__ = "$Id: util.py,v 1.43 2000/08/08 14:38:13 gward Exp $"
  9.  
  10. import sys, os, string, re, shutil
  11. from distutils.errors import *
  12. from distutils.spawn import spawn
  13.  
  14.  
  15. def get_platform ():
  16.     """Return a string (suitable for tacking onto directory names) that
  17.     identifies the current platform.  Currently, this is just
  18.     'sys.platform'.
  19.     """
  20.     return sys.platform
  21.  
  22.  
  23. def convert_path (pathname):
  24.     """Return 'pathname' as a name that will work on the native
  25.        filesystem, i.e. split it on '/' and put it back together again
  26.        using the current directory separator.  Needed because filenames in
  27.        the setup script are always supplied in Unix style, and have to be
  28.        converted to the local convention before we can actually use them in
  29.        the filesystem.  Raises ValueError if 'pathname' is
  30.        absolute (starts with '/') or contains local directory separators
  31.        (unless the local separator is '/', of course)."""
  32.  
  33.     if pathname[0] == '/':
  34.         raise ValueError, "path '%s' cannot be absolute" % pathname
  35.     if pathname[-1] == '/':
  36.         raise ValueError, "path '%s' cannot end with '/'" % pathname
  37.     if os.sep != '/':
  38.         paths = string.split (pathname, '/')
  39.         return apply (os.path.join, paths)
  40.     else:
  41.         return pathname
  42.  
  43. # convert_path ()
  44.  
  45.  
  46. def change_root (new_root, pathname):
  47.     """Return 'pathname' with 'new_root' prepended.  If 'pathname' is
  48.     relative, this is equivalent to "os.path.join(new_root,pathname)".
  49.     Otherwise, it requires making 'pathname' relative and then joining the
  50.     two, which is tricky on DOS/Windows and Mac OS.
  51.     """
  52.     if os.name == 'posix':
  53.         if not os.path.isabs (pathname):
  54.             return os.path.join (new_root, pathname)
  55.         else:
  56.             return os.path.join (new_root, pathname[1:])
  57.  
  58.     elif os.name == 'nt':
  59.         (drive, path) = os.path.splitdrive (pathname)
  60.         if path[0] == '\\':
  61.             path = path[1:]
  62.         return os.path.join (new_root, path)
  63.  
  64.     elif os.name == 'mac':
  65.         raise RuntimeError, "no clue how to do this on Mac OS"
  66.  
  67.     else:
  68.         raise DistutilsPlatformError, \
  69.               "nothing known about platform '%s'" % os.name
  70.  
  71.  
  72. _environ_checked = 0
  73. def check_environ ():
  74.     """Ensure that 'os.environ' has all the environment variables we
  75.        guarantee that users can use in config files, command-line
  76.        options, etc.  Currently this includes:
  77.          HOME - user's home directory (Unix only)
  78.          PLAT - description of the current platform, including hardware
  79.                 and OS (see 'get_platform()')
  80.     """
  81.  
  82.     global _environ_checked
  83.     if _environ_checked:
  84.         return
  85.  
  86.     if os.name == 'posix' and not os.environ.has_key('HOME'):
  87.         import pwd
  88.         os.environ['HOME'] = pwd.getpwuid (os.getuid())[5]
  89.  
  90.     if not os.environ.has_key('PLAT'):
  91.         os.environ['PLAT'] = get_platform ()
  92.  
  93.     _environ_checked = 1
  94.  
  95.  
  96. def subst_vars (str, local_vars):
  97.     """Perform shell/Perl-style variable substitution on 'string'.
  98.        Every occurrence of '$' followed by a name, or a name enclosed in
  99.        braces, is considered a variable.  Every variable is substituted by
  100.        the value found in the 'local_vars' dictionary, or in 'os.environ'
  101.        if it's not in 'local_vars'.  'os.environ' is first checked/
  102.        augmented to guarantee that it contains certain values: see
  103.        '_check_environ()'.  Raise ValueError for any variables not found in
  104.        either 'local_vars' or 'os.environ'."""
  105.  
  106.     check_environ ()
  107.     def _subst (match, local_vars=local_vars):
  108.         var_name = match.group(1)
  109.         if local_vars.has_key (var_name):
  110.             return str (local_vars[var_name])
  111.         else:
  112.             return os.environ[var_name]
  113.  
  114.     return re.sub (r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, str)
  115.  
  116. # subst_vars ()
  117.  
  118.  
  119. def grok_environment_error (exc, prefix="error: "):
  120.     """Generate a useful error message from an EnvironmentError (IOError or
  121.     OSError) exception object.  Handles Python 1.5.1 and 1.5.2 styles, and
  122.     does what it can to deal with exception objects that don't have a
  123.     filename (which happens when the error is due to a two-file operation,
  124.     such as 'rename()' or 'link()'.  Returns the error message as a string
  125.     prefixed with 'prefix'.
  126.     """
  127.     # check for Python 1.5.2-style {IO,OS}Error exception objects
  128.     if hasattr (exc, 'filename') and hasattr (exc, 'strerror'):
  129.         if exc.filename:
  130.             error = prefix + "%s: %s" % (exc.filename, exc.strerror)
  131.         else:
  132.             # two-argument functions in posix module don't
  133.             # include the filename in the exception object!
  134.             error = prefix + "%s" % exc.strerror
  135.     else:
  136.         error = prefix + str(exc[-1])
  137.  
  138.     return error
  139.  
  140.  
  141. # Needed by 'split_quoted()'
  142. _wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace)
  143. _squote_re = re.compile(r"'(?:[^'\\]|\\.)*'")
  144. _dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"')
  145.  
  146. def split_quoted (s):
  147.     """Split a string up according to Unix shell-like rules for quotes and
  148.     backslashes.  In short: words are delimited by spaces, as long as those
  149.     spaces are not escaped by a backslash, or inside a quoted string.
  150.     Single and double quotes are equivalent, and the quote characters can
  151.     be backslash-escaped.  The backslash is stripped from any two-character
  152.     escape sequence, leaving only the escaped character.  The quote
  153.     characters are stripped from any quoted string.  Returns a list of
  154.     words.
  155.     """
  156.  
  157.     # This is a nice algorithm for splitting up a single string, since it
  158.     # doesn't require character-by-character examination.  It was a little
  159.     # bit of a brain-bender to get it working right, though...
  160.  
  161.     s = string.strip(s)
  162.     words = []
  163.     pos = 0
  164.  
  165.     while s:
  166.         m = _wordchars_re.match(s, pos)
  167.         end = m.end()
  168.         if end == len(s):
  169.             words.append(s[:end])
  170.             break
  171.  
  172.         if s[end] in string.whitespace: # unescaped, unquoted whitespace: now
  173.             words.append(s[:end])       # we definitely have a word delimiter
  174.             s = string.lstrip(s[end:])
  175.             pos = 0
  176.  
  177.         elif s[end] == '\\':            # preserve whatever is being escaped;
  178.                                         # will become part of the current word
  179.             s = s[:end] + s[end+1:]
  180.             pos = end+1
  181.  
  182.         else:
  183.             if s[end] == "'":           # slurp singly-quoted string
  184.                 m = _squote_re.match(s, end)
  185.             elif s[end] == '"':         # slurp doubly-quoted string
  186.                 m = _dquote_re.match(s, end)
  187.             else:
  188.                 raise RuntimeError, \
  189.                       "this can't happen (bad char '%c')" % s[end]
  190.  
  191.             if m is None:
  192.                 raise ValueError, \
  193.                       "bad string (mismatched %s quotes?)" % s[end]
  194.  
  195.             (beg, end) = m.span()
  196.             s = s[:beg] + s[beg+1:end-1] + s[end:]
  197.             pos = m.end() - 2
  198.  
  199.         if pos >= len(s):
  200.             words.append(s)
  201.             break
  202.  
  203.     return words
  204.  
  205. # split_quoted ()
  206.  
  207.  
  208. def execute (func, args, msg=None, verbose=0, dry_run=0):
  209.     """Perform some action that affects the outside world (eg.  by writing
  210.     to the filesystem).  Such actions are special because they are disabled
  211.     by the 'dry_run' flag, and announce themselves if 'verbose' is true.
  212.     This method takes care of all that bureaucracy for you; all you have to
  213.     do is supply the function to call and an argument tuple for it (to
  214.     embody the "external action" being performed), and an optional message
  215.     to print.
  216.     """
  217.     # Generate a message if we weren't passed one
  218.     if msg is None:
  219.         msg = "%s%s" % (func.__name__, `args`)
  220.         if msg[-2:] == ',)':        # correct for singleton tuple 
  221.             msg = msg[0:-2] + ')'
  222.  
  223.     # Print it if verbosity level is high enough
  224.     if verbose:
  225.         print msg
  226.  
  227.     # And do it, as long as we're not in dry-run mode
  228.     if not dry_run:
  229.         apply(func, args)
  230.  
  231. # execute()
  232.